﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections.Generic;

namespace LotteCinema
{
    public class CDatastore
    {
        # region ConnectionString

        public static string _connectionString = @"Data Source=THANG-PC\sqlexpress;Initial Catalog=QuanLyRapPhim1;Integrated Security=True";

        protected static string ConnectionString
        {
            get { return CDatastore._connectionString; }
        }

        # endregion

        #region DocStore 

        public static DataTable DocStore(String spName, List<SqlParameter> sqlParams) //ExecuteQuery
        {
            DataTable dt = new DataTable();
            try
            {
                SqlConnection connect = new SqlConnection(ConnectionString);
                connect.Open();
                try
                {
                    SqlCommand command = connect.CreateCommand();
                    command.CommandType = CommandType.StoredProcedure;
                    command.CommandText = spName;
                    if (sqlParams != null)
                    {
                        foreach (SqlParameter param in sqlParams)
                        {
                            command.Parameters.Add(param);
                        }
                    }
                    SqlDataAdapter adapter = new SqlDataAdapter();
                    adapter.SelectCommand = command;
                    adapter.Fill(dt);
                }

                catch (SqlException ex)
                {
                    throw ex;
                }
                finally
                {
                    connect.Close();
                }
            }

            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public static DataTable DocStore(String spName)
        {
            return DocStore(spName, null);
        }

        #endregion

        #region ExecuteNoneQuery

        public static int ThucHienStore(String spName, List<SqlParameter> sqlParams) //ExecuteNoneQuery
        {
            int n;
            try
            {
                SqlConnection connect = new SqlConnection(ConnectionString);
                connect.Open();
                try
                {
                    SqlCommand command = connect.CreateCommand();
                    command.CommandType = CommandType.StoredProcedure;
                    command.CommandText = spName;
                    if (sqlParams != null)
                    {
                        foreach (SqlParameter param in sqlParams)
                        {
                            command.Parameters.Add(param);
                        }
                    }
                    n = command.ExecuteNonQuery();
                }

                catch (SqlException ex)
                {
                    throw ex;
                }
                finally
                {
                    connect.Close();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return n;
        }

        public static int ThucHienStore(String spName)
        {
            return ThucHienStore(spName, null);
        }
        #endregion

        /*

        //Cách gọi
        public DataTable LietKeLichTrinh()
        {
            try
            {
                return SqlDataAccessHelper.ExecuteQuery("sp_LietKeLichTrinh");
            }
            catch (Exception ex) { throw ex; }
        }
        public void SapVe(String MaTau, String MaChuyen, String MaLT)
        {
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@matau", MaTau));
                sqlParams.Add(new SqlParameter("@machuyen", MaChuyen));
                sqlParams.Add(new SqlParameter("@malt", MaLT));
                SqlDataAccessHelper.ExecuteNoneQuery("sp_CungCapVe", sqlParams);
            }
            catch (Exception ex) { throw ex; }
         * */
        }
   }